/**
 * Contains custom JavaScript code
 */
var urlHolderTraining = new Object();

function loadTableTraining() {
	$.get(urlHolderTraining.records, function(response) {
		
 		$('#tableTraining').find('tbody').remove();
 		
 		for (var i=0; i<response.trainings.length; i++) {
			var row = '<tr>';
			row += '<td><input type="radio" name="index" id="index" value="'+i+'"></td>';
			row += '<td>' + response.trainings[i].trainingId + '</td>';
			row += '<td>' + response.trainings[i].trainingName + '</td>';
			row += '<td>' + response.trainings[i].description + '</td>';
			row += '</tr>';
	 		$('#tableTraining').append(row);
 		}
 		
 		$('#tableTraining').data('model', response.trainings);
		//toggleForms('hide'); 
 	});
	toggleFormsTraining('hide');
}

function submitNewRecordTraining() {
	$.post(urlHolderTraining.add, {
			trainingId: $('#newTrainingId').val(),
			trainingName: $('#newTrainingName').val(),
			description: $('#newDescription').val(),
		}, 
		function(response) {
			if (!response == null) {
				loadTableTraining();
				toggleFormsTraining('hide'); ;
				toggleCrudButtonsTraining('show');
				alert('Success! Record has been added.');
			} else {
				alert('Record Already Exists !');
			}
		}
	);	
}

function submitDeleteRecordTraining() {
	var selected = $('input:radio[name=index]:checked').val();
	
	$.post(urlHolderTraining.del, {
			trainingId: $('#tableTraining').data('model')[selected].trainingId
		}, 
		function(response) {
			if (response == true) {
				loadTableTraining(urlHolderTraining.records);
				alert('Success! Record has been deleted.');
			} else {
				alert('Failure! An error has occurred!');
			}
		}
	);
}

function submitUpdateRecordTraining() {
	$.post(urlHolderTraining.edit, {
			trainingId: $('#editTrainingId').val(),
			trainingName: $('#editTrainingName').val(),
			description: $('#editDescription').val(),
		}, 
		function(response) {
			if (response != null) {
				loadTableTraining();
				toggleFormsTraining('hide'); ;
				toggleCrudButtonsTraining('show');
				alert('Success! Record has been edited.');
			} else {
				alert('Failure! An error has occurred!');
			}
		}
	);
}

function getRole(role) {
	if (role == 1) {
		return 'Trainee';
	} 
	else if (role == 2) {
		return 'Junior Engineer';
	}
	 else if (role == 3) {
			return 'Software Engineer';
		}
	 else if (role == 4) {
			return 'Sr. Software Engineer';
		}
	 else if (role == 5) {
			return 'Lead';
		}
	 else if (role == 6) {
			return 'Sr. Lead';
		}
	 else if (role == 7) {
			return 'Technical Specialist';
		}
	 else if (role == 8) {
			return 'Architect';
		}
	 else if (role == 9) {
			return 'Manager';
		}
	 else if (role == 10) {
			return 'Sr. Manager';
		}
	 else {
		return 'Unknown';
	} 
}


function hasSelectedTraining() {
	var selected = $('input:radio[name=index]:checked').val();
	if (selected == undefined) { 
		alert('Select a record first!');
		return false;
	}
	
	return true;
}

function fillEditFormTraining() {
	var selected = $('input:radio[name=index]:checked').val();
	$('#editTrainingId').val( $('#tableTraining').data('model')[selected].trainingId );
	$('#editTrainingName').val( $('#tableTraining').data('model')[selected].trainingName );
	$('#editDescription').val( $('#tableTraining').data('model')[selected].description );
}

function resetNewFormTraining() {
	$('#newUsername').val('');
	$('#newPassword').val('');
	$('#newFirstName').val('');
	$('#newLastName').val('');
	$('#newRole').val('2');
}

function resetEditFormTraining() {
	$('#editFirstName').val('');
	$('#editLastName').val('');
	$('#editRole').val('2');
}

function toggleFormsTraining(id) {
	//alert(id);
	if (id == 'hide') {
		$('#newFormTraining').hide();
		$('#editFormTraining').hide();
		
	} else if (id == 'new') {
 		resetNewFormTraining();
 		$('#newFormTraining').show();
 		$('#editFormTraining').hide();
 		
	} else if (id == 'edit') {
 		resetEditFormTraining();
 		$('#newFormTraining').hide();
 		$('#editFormTraining').show();
	}
}

function toggleCrudButtonsTraining(id) {
	if (id == 'show') {
		$('#newBtnTraining').removeAttr('disabled');
		$('#editBtnTraining').removeAttr('disabled');
		$('#deleteBtnTraining').removeAttr('disabled');
		$('#reloadBtnTraining').removeAttr('disabled');
		
	} else if (id == 'hide'){
		$('#newBtnTraining').attr('disabled', 'disabled');
		$('#editBtnTraining').attr('disabled', 'disabled');
		$('#deleteBtnTraining').attr('disabled', 'disabled');
		$('#reloadBtnTraining').attr('disabled', 'disabled');
	}
}
